<template>
  <div class="app-container">
    <div class="btn-add">
      <el-button type="primary" size="mini" @click="addModel">新增</el-button>
    </div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item prop="checkTableName">
        <el-input v-model="queryParams.checkTableName" placeholder="请输入标准名称" clearable size="small" style="width: 200px" />
      </el-form-item>
      <el-form-item prop="checkTableId">
        <el-input v-model="queryParams.checkTableId" placeholder="请输入标准ID" clearable size="small" style="width: 200px" />
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.optRoleId" placeholder="请选择角色" clearable size="small" style="width: 200px">
          <el-option
            v-for="dict in roleList"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="projectName">
        <div class="project-select" @click="selectProject">
          <span>
            <span style="color: #606266;" v-if="queryParams.projectName">{{queryParams.projectName}}</span>
            <span style="color: #C0C4CC;" v-else>请选择项目名称</span>
          </span>
          <span v-if="queryParams.projectName" class="icon" style="color: #C0C4CC;" @click.stop="clearProject"><i class="el-icon-circle-close"></i></span>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="configList">
      <el-table-column label="项目ID" align="center" prop="projectId" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="标准ID" align="center" prop="checkTableId" />
      <el-table-column label="标准名称" align="center" prop="checkName" />
      <el-table-column label="频次" align="center" prop="frequency" />
      <el-table-column label="合格标准" align="center" prop="passingScore" />
      <el-table-column label="适用角色" align="center" prop="optRoleNameList" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <span>{{scope.row.status === 0 ? '停用' : '启用'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-if="scope.row.status === 0" @click="startUser(scope.row)">启用</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" v-else @click="stopUser(scope.row)">停用</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" :disabled="scope.row.status === 1" @click="editTable(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :disabled="scope.row.status === 1" @click="deleteProject(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />
    <add-or-edit-modal :roleList="roleList" ref="addOrEditModal" @update="updateList" />
    <ProjectConfigModal ref="projectConfigRef" @update="changeProject" />
  </div>
</template>
<script>
import addOrEditModal from './addOrEditModal.vue'
import ProjectConfigModal from './projectConfigModal.vue'
import { getRoleList, getProjectTableList, changeUseStatus, deleteProjectApi } from '@/api/auditManagement/projectConfig'
export default {
  name: "ProjectConfig",
  components: {
    addOrEditModal,
    ProjectConfigModal
  },
  data () {
    return {
      roleList: [],
      projectList: [],
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        checkTableName: '',
        checkTableId: '',
        optRoleId: '',
        projectName: ''
      },
    };
  },
  created () {
    let token = this.$route.query
    if(token) {
      if(token.icsAuthorization) {
        localStorage.setItem('icsAuthorization', decodeURIComponent(token.icsAuthorization))
      }
      if(token.authorization) {
        localStorage.setItem('authorization', decodeURIComponent(token.authorization))
      }
    }
    this.getRoles()
    this.getList()
  },
  methods: {
    getRoles(){
      getRoleList().then(res => {
        this.roleList = res
      })
    },
    getList () {
      let params = {
        ...this.pagination
      }
      if(this.queryParams.checkTableName) {
        params.checkTableName = this.queryParams.checkTableName
      }
      if(this.queryParams.checkTableId) {
        params.checkTableId = this.queryParams.checkTableId
      }
      if(this.queryParams.optRoleId) {
        params.optRoleId = this.queryParams.optRoleId
      }
      if(this.queryParams.projectName) {
        params.projectName = this.queryParams.projectName
      }
      getProjectTableList(params).then(res => {
        this.configList = res.rows
        this.total = res.total
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.pagination.pageNum = 1
      this.getList()
    },
    addModel() {
      this.$refs.addOrEditModal.init()
    },
    selectProject(){
      this.$refs.projectConfigRef.getInit()
    },
    changeProject(item){
      this.queryParams.projectName = item.name
    },
    clearProject(){
      this.queryParams.projectName = ''
    },
    startUser(row){
      changeUseStatus(row.projectCheckTableId).then(res => {
        this.msgSuccess("已启用");
        this.getList();
      })
    },
    stopUser(row){
      changeUseStatus(row.projectCheckTableId).then(res => {
        this.msgSuccess("已停用");
        this.getList();
      })
    },
    editTable(row){
      this.$refs.addOrEditModal.init(row)
    },
    updateList() {
      this.getList();
    },
    deleteProject(row){
      this.$confirm('是否确认删除稽核标准', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return deleteProjectApi(row.projectCheckTableId);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.queryParams = {
        checkTableName: '',
        checkTableId: '',
        optRoleId: '',
        projectName: ''
      }
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
      }
      this.projectName = ''
      this.handleQuery()
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{
  padding-top: 35px;
  position: relative;
  .btn-add{
    position: absolute;
    right: 10px;
    top: 10px;
  }
  .el-form-item{
    margin-bottom: 10px;
  }
}
.project-select{
  width: 200px;
  height: 34px;
  background: #FFFFFF;
  border-radius: 5px;
  color: #C0C4CC;
  padding: 0 20px;
  cursor: pointer;
  border: 1px solid #DCDFE6;
  position: relative;
  overflow: hidden;
  .icon{
    position: absolute;
    right: 5px;
    top: 0;
  }
}
</style>
